package com.gimi.cloud.jimi.commonService.ueditor.imp;

import com.baidu.ueditor.define.BaseState;
import com.baidu.ueditor.define.MultiState;
import com.baidu.ueditor.define.State;
import com.gimi.cloud.jimi.common.utils.UUIDUtil;
import com.gimi.cloud.jimi.common.utils.oss.AliOSSUtil;
import com.gimi.cloud.jimi.commonService.ueditor.UeditorUpload;
import com.nimbusds.jose.util.Base64;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class UeditorUploadImp implements UeditorUpload {


	@Override
	public State uploadImage(HttpServletRequest requst,String actionType,Map<String, Object> conf) {
		System.out.println(conf);
		System.out.println("上传uploadImage");
		State state = null;
		try {
			//获取配置的图片上传路径，自动按照当前日期进行存储
			DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
			String dirPath = conf.get("savePath").toString()+dateFormat.format(new Date())+File.separator;
			//String dirPath = conf.get("savePath").toString();
			if(dirPath.indexOf("/")==0){
				dirPath = dirPath.substring(1);
			}
			//涂鸦等操作，base64进行上传
			if ("true".equals(conf.get("isBase64"))){
				String base64 = requst.getParameter(conf.get("fieldName").toString());
				InputStream is = new ByteArrayInputStream(java.util.Base64.getDecoder().decode(base64));
				String uuId = UUIDUtil.get32UUID();
				String fileName = uuId + "." + "jpg";
				String result = AliOSSUtil.uploadFile(is, dirPath, fileName);
				String tempUrl = AliOSSUtil.getFileUrl(result, new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24));
				state = new BaseState(true, "上传成功");//如果第一个参数为false则第二个参数为失败信息
				state.putInfo("url", tempUrl);
				state.putInfo("type", "jpg");
				state.putInfo("original", "");
			}else {
				StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) requst;
				MultiValueMap multipartFiles = multipartHttpServletRequest.getMultiFileMap();
				for (Object entry : multipartFiles.entrySet()) {
					LinkedList<MultipartFile> lists = ((Map.Entry<String, LinkedList<MultipartFile>>) entry).getValue();
					for (MultipartFile file : lists) {
						InputStream is = file.getInputStream();
						String uuId = UUIDUtil.get32UUID();
						String[] originalFilenames = file.getOriginalFilename().split("\\.");
						String fileName = uuId + "." + originalFilenames[originalFilenames.length - 1];
						String result = AliOSSUtil.uploadFile(is, dirPath, fileName);
						String tempUrl = AliOSSUtil.getFileUrl(result, new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24));

						state = new BaseState(true, "上传成功");//如果第一个参数为false则第二个参数为失败信息
						state.putInfo("original", file.getOriginalFilename());//原始文件名
						state.putInfo("size", file.getSize());//文件大小
						state.putInfo("title", fileName);//文件名
						state.putInfo("type", "." + originalFilenames[originalFilenames.length - 1]);//文件后缀名
						state.putInfo("url", tempUrl);//访问地址
					}
				}
			}
		}catch (Exception e){
			e.printStackTrace();
			state = new BaseState(false, "上传阿里OSS失败");//如果第一个参数为false则第二个参数为失败信息
		}
		return state;
	}

	@Override
	public State catchImage(HttpServletRequest requst,String actionType,Map<String, Object> conf) {
		System.out.println("上传catchImage");
		return null;
	}

	@Override
	public State listImage(HttpServletRequest requst,String actionType,Map<String, Object> conf) {
		System.out.println(conf);
		System.out.println("获取图片、文件列表");
		//获取配置的图片上传路径，自动按照当前日期进行存储
		DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		String dirPath = conf.get("dir").toString()+dateFormat.format(new Date())+File.separator;
		if(dirPath.indexOf("/")==0){
			dirPath = dirPath.substring(1);
		}
		List<String> files = AliOSSUtil.getDirFileList(dirPath,true, Integer.parseInt(conf.get("size").toString()));
		MultiState state = new MultiState(true,"");;
		for(String file : files){
			State tempState = new BaseState(true,"");
			tempState.putInfo("url",AliOSSUtil.getFileUrl(file,AliOSSUtil.getNoEffictDateByYear(100)));
			state.addState(tempState);
		}
		state.putInfo("start",conf.get("start").toString());
		state.putInfo("total",files.size());
		return state;
	}

}
